/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.swin.swinvehicle.corporateclient;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Administrator
 */
@Stateless
public class CorporateClientManagerBean implements CorporateClientManagerRemote {

    @PersistenceContext
    private EntityManager em;

    public void addCorporateClient(CorporateClientDetailedTO to) throws CorporateClientManagerException {
        try {
            CorporateClientEntity cc = new CorporateClientEntity();
            cc.setData(to);
            cc.setDeleted(Boolean.FALSE);
            this.em.persist(cc);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CorporateClientManagerException("Cannot add corporate client", e);
        }
    }

    public void editCorporateClient(CorporateClientDetailedTO to) throws CorporateClientManagerException {
        try {
            CorporateClientEntity cc = this.em.find(CorporateClientEntity.class, to.getId());
            cc.setData(to);
            this.em.merge(cc);
        } catch (Exception e) {
            throw new CorporateClientManagerException("Cannot edit corporate client", e);
        }
    }

    public void editCorporateClientDetails(CorporateClientDetailedTO to) throws CorporateClientManagerException {
        try {
            CorporateClientEntity cc = this.em.find(CorporateClientEntity.class, to.getId());
            cc.setDetails(to);
            this.em.merge(cc);
        } catch (Exception e) {
            throw new CorporateClientManagerException("Cannot edit corporate client", e);
        }
    }

    public void deleteCorporateClient(Long id) throws CorporateClientManagerException {
        try {
            CorporateClientEntity cc = this.em.find(CorporateClientEntity.class, id);
            cc.setDeleted(Boolean.TRUE);
            this.em.merge(cc);
        } catch (Exception e) {
            throw new CorporateClientManagerException("Cannot delete corporate client", e);
        }
    }

    public CorporateClientDetailedTO getCorporateClient(Long id) throws CorporateClientManagerException {
        try {
            CorporateClientEntity cc = em.find(CorporateClientEntity.class, id);
            return cc.getDetailedData();
        } catch (Exception e) {
            throw new CorporateClientManagerException("Cannot get corporate client by ID " + id.longValue(), e);
        }
    }

    public List<CorporateClientTO> getAllCorporateClients() throws CorporateClientManagerException {
        try {
            List<CorporateClientTO> results = new ArrayList<CorporateClientTO>();
            Query allCorporateClientsQuery = em.createNamedQuery("findAllCorporateClients");
            List dbResults = allCorporateClientsQuery.getResultList();
            for (int i = 0; i < dbResults.size(); i++) {
                CorporateClientEntity d = (CorporateClientEntity) dbResults.get(i);
                results.add(d.getData());
            }
            return results;
        } catch (Exception e) {
            throw new CorporateClientManagerException("Cannot get all corporate clients", e);
        }
    }

    public List<CorporateClientDetailedTO> getAllDetailedCorporateClients() throws CorporateClientManagerException {
        try {
            List<CorporateClientDetailedTO> results = new ArrayList<CorporateClientDetailedTO>();
            Query allCorporateClientsQuery = em.createNamedQuery("findAllCorporateClients");
            List dbResults = allCorporateClientsQuery.getResultList();
            for (int i = 0; i < dbResults.size(); i++) {
                CorporateClientEntity d = (CorporateClientEntity) dbResults.get(i);
                results.add(d.getDetailedData());
            }
            return results;
        } catch (Exception e) {
            throw new CorporateClientManagerException("Cannot get all detailed corporate clients", e);
        }
    }

    public boolean isUsernameAvailable(String username) throws CorporateClientManagerException {
        try {
            Query findDriverByUsernameQuery = this.em.createNamedQuery("findCorporateClientByUsername");
            findDriverByUsernameQuery.setParameter("username", username);

            List corporateClients = findDriverByUsernameQuery.getResultList();
            if (corporateClients != null && corporateClients.size() != 0) {
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            throw new CorporateClientManagerException("Cannot check if the username exist", e);
        }
    }

    /**
     * Authenticate driver using username and password
     * @param username
     * @param password
     * @return Driver ID
     * @throws au.edu.swin.swinvehicle.driver.DriverManagerException
     */
    public Long authenticate(String username, String password) throws CorporateClientManagerException {
        try {
            Query findCorporateClientByUsernameQuery = this.em.createNamedQuery("findCorporateClientByUsername");
            findCorporateClientByUsernameQuery.setParameter("username", username);

            List corporateClients = findCorporateClientByUsernameQuery.getResultList();
            if (corporateClients != null && corporateClients.size() > 0) {
                CorporateClientEntity corporateClient = (CorporateClientEntity) corporateClients.get(0);

                if (corporateClient.getPassword().equals(password)) {
                    return corporateClient.getId();
                } else {
                    return null;
                }
            } else {
                return null;
            }
        } catch (Exception e) {
            throw new CorporateClientManagerException("Cannot authenticate corporate client with username " + username, e);
        }
    }
}
